﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class TLCongNoTraKhongVAT : System.Web.UI.Page
{
    SqlParameter[] p;
    clsXuLy ex = new clsXuLy();
    string frmName = "frmTLCongNoTraKhongVAT";
    static int f_SuDung = 0, f_Them = 0, f_Sua = 0, f_Xoa = 0, f_XemTatCa = 0, f_InBaoCao = 0;
    public string Header = System.Configuration.ConfigurationSettings.AppSettings["Header"].ToString();
    static DataTable dt;
    static int PageNumber = 10;
    static int CurrentPage = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Convert.ToString(Session["user"])))
            gstGetMess("Hết phiên làm việc, vui lòng đăng nhập để tiếp tục", "Default.aspx");
        else
        {
            if (!IsPostBack)
            {

                btnCancel_Click(sender, e);

                Title = "Thiết lập công nợ trả không vat | " + Header;

                string idnguoidung = Session["IDUser"].ToString();
                string sql = "select SuDung,Them,Sua,Xoa,InBaoCao,XemTatCa from XDAPhanQuyen where IDnguoidung='" + idnguoidung + "' and tag='" + frmName + "'";

                DataTable dt2 = ex.GetData_Text(sql);
                if (dt2.Rows.Count > 0)
                {
                    f_SuDung = int.Parse(dt2.Rows[0]["SuDung"].ToString());
                    f_Them = int.Parse(dt2.Rows[0]["Them"].ToString());
                    f_Sua = int.Parse(dt2.Rows[0]["Sua"].ToString());
                    f_Xoa = int.Parse(dt2.Rows[0]["Xoa"].ToString());
                    f_InBaoCao = int.Parse(dt2.Rows[0]["InBaoCao"].ToString());
                    f_XemTatCa = int.Parse(dt2.Rows[0]["XemTatCa"].ToString());
                }
                if (f_SuDung.Equals(1))
                {
                    btnCancel_Click(sender, e);
                    int year1 = DateTime.Now.Year - 3;
                    int year2 = DateTime.Now.Year;
                    for (int i = year2; i >= year1; i--)
                    {
                        dlNam.Items.Add(new ListItem("Năm " + i.ToString(), i.ToString()));
                        drnam.Items.Add(new ListItem("Năm " + i.ToString(), i.ToString()));
                    }
                    dlNam.SelectedValue = year2.ToString();
                    LoadNCCTK();
                }
                else
                    gstGetMess("Bạn không có quyền xem trang này", "Home.aspx");

            }
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        LoadNhaCungCap();
        txtsotienthanhtoan.Text = "0";
        drnganhang.Visible = false;
        drtaikhoan.Visible = false;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdathanhtoancongnotra_ListKhongVAT", p);
        GV.DataBind();
    }
    private void gstGetMess(string gstMess, string gstLink)
    {
        if (gstLink == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

    }
    void LoadNhaCungCap()
    {
        drnhacungcap.Items.Clear();
        drnhacungcap.DataSource = ex.GetData("sp_ThanhToanCN_NhaCungCap_ListAll");
        drnhacungcap.DataBind();
        drnhacungcap.Items.Insert(0, new ListItem("--Chọn nhà cung cấp--", ""));
        drnhacungcap.SelectedIndex = 0;
    }
    void LoadNganHang()
    {
        drnganhang.Items.Clear();
        drnganhang.DataSource = ex.GetData("sp_xdaNganHang_ThanhToanCongNoTraVAT_List");
        drnganhang.DataBind();
        drnganhang.Items.Insert(0, new ListItem("--Chọn ngân hàng--", ""));
        drnganhang.SelectedIndex = 0;
    }
    void LoadNCCTK()
    {
        dlnhacungcap.Items.Clear();
        dlnhacungcap.DataSource = ex.GetData("[sp_ThanhToanCN_NhaCungCap_TK]");
        dlnhacungcap.DataBind();
        dlnhacungcap.Items.Insert(0, new ListItem("--Chọn nhà cung cấp--", ""));
        dlnhacungcap.SelectedIndex = 0;
    }
    void LoadTaiKhoan()
    {
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@NganHang", ex.GetGuid(drnganhang.SelectedValue));
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
        drtaikhoan.Items.Clear();
        drtaikhoan.DataSource = ex.GetData("sp_xdaThanhToanCongNoTraVAT_ListTaiKhoan", p);
        drtaikhoan.DataBind();
        drtaikhoan.Items.Insert(0, new ListItem("--Chọn tài khoản--", ""));
        drtaikhoan.SelectedIndex = 0;
    }
    protected void dlThang_SelectedIndexChanged(object sender, EventArgs e)
    {
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@Thang", dlThang.SelectedValue);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@Nam", dlNam.SelectedValue);
        p[1].SqlDbType = SqlDbType.Int;
        drnhacungcap.Items.Clear();
        drnhacungcap.DataSource = ex.GetData("sp_ThanhToanCN_NhaCungCap", p);
        drnhacungcap.DataBind();
        drnhacungcap.Items.Insert(0, new ListItem("--Chọn nhà cung cấp--", ""));
        drnhacungcap.SelectedIndex = 0;
        drnhacungcap_SelectedIndexChanged(sender, e);
    }
    protected void drnhacungcap_SelectedIndexChanged(object sender, EventArgs e)
    {
        p = new SqlParameter[3];
        p[0] = new SqlParameter("@NhaCungCap", ex.GetGuid(drnhacungcap.SelectedValue));
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
        p[1] = new SqlParameter("@Thang", dlThang.SelectedValue);
        p[1].SqlDbType = SqlDbType.Int;
        p[2] = new SqlParameter("@Nam", dlNam.SelectedValue);
        p[2].SqlDbType = SqlDbType.Int;
        dt = ex.GetData("sp_TienHangThangNay_CongNo_KhongVAT", p);
        if (dt.Rows[0]["gia"].ToString().Equals(""))
            txttienhang.Text = "0";
        else
            txttienhang.Text = dt.Rows[0]["gia"].ToString();

        p[0] = new SqlParameter("@NhaCungCap", ex.GetGuid(drnhacungcap.SelectedValue));
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
        p[1] = new SqlParameter("@Thang", dlThang.SelectedValue);
        p[1].SqlDbType = SqlDbType.Int;
        p[2] = new SqlParameter("@Nam", dlNam.SelectedValue);
        p[2].SqlDbType = SqlDbType.Int;
        dt = ex.GetData("sp_TienCuocVCKhongVATThangNay_CongNo", p);
        if (dt.Rows[0]["giacuoc"].ToString().Equals(""))
            txttiencuoc.Text = "0";
        else
            txttiencuoc.Text = dt.Rows[0]["giacuoc"].ToString();

        p[0] = new SqlParameter("@NhaCungCap", ex.GetGuid(drnhacungcap.SelectedValue));
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
        p[1] = new SqlParameter("@Thang", dlThang.SelectedValue);
        p[1].SqlDbType = SqlDbType.Int;
        p[2] = new SqlParameter("@Nam", dlNam.SelectedValue);
        p[2].SqlDbType = SqlDbType.Int;
        dt = ex.GetData("sp_ThanhToan_SoDuKiTruocKhongVAT_CongNoTra", p);
        txtsodukitruoc.Text = dt.Rows[0]["sodukitruoc"].ToString();

        p[0] = new SqlParameter("@NhaCungCap", ex.GetGuid(drnhacungcap.SelectedValue));
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
        p[1] = new SqlParameter("@Thang", dlThang.SelectedValue);
        p[1].SqlDbType = SqlDbType.Int;
        p[2] = new SqlParameter("@Nam", dlNam.SelectedValue);
        p[2].SqlDbType = SqlDbType.Int;
        dt = ex.GetData("f_sp_SoTienThanhToanKyNay_CongNoTraKhongVAT", p);
        txtsotiendathanhtoan.Text = dt.Rows[0]["sotiendathanhtoan"].ToString();

        double thanhtienkynay;
        thanhtienkynay = double.Parse(txttiencuoc.Text) + double.Parse(txttienhang.Text);
        txtthanhtienkynay.Text = thanhtienkynay.ToString();
        double sotienthanhtoan;
        sotienthanhtoan = double.Parse(txtsotienthanhtoan.Text); ;
        double sotienconlai;
        sotienconlai = thanhtienkynay + double.Parse(txtsodukitruoc.Text) - sotienthanhtoan - double.Parse(txtsotiendathanhtoan.Text);
        txtsotienconlai.Text = sotienconlai.ToString();
    }
    protected void txtsotienthanhtoan_TextChanged(object sender, EventArgs e)
    {
        double thanhtienkynay;
        thanhtienkynay = double.Parse(txtthanhtienkynay.Text);

        double sotienthanhtoan;
        sotienthanhtoan = double.Parse(txtsotienthanhtoan.Text); ;
        double sotienconlai;
        sotienconlai = thanhtienkynay + double.Parse(txtsodukitruoc.Text) - sotienthanhtoan - double.Parse(txtsotiendathanhtoan.Text);
        txtsotienconlai.Text = sotienconlai.ToString();
    }
    protected void drhinhthucthanhtoan_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drhinhthucthanhtoan.SelectedValue == "1")
        {
            LoadNganHang();
            drnganhang.Visible = true;
        }
        else
        {
            drnganhang.Visible = false;
            drtaikhoan.Visible = false;
        }
    }
    protected void drnganhang_SelectedIndexChanged(object sender, EventArgs e)
    {

        LoadTaiKhoan();
        drtaikhoan.Visible = true;

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (f_Them.Equals(1))
        {
            try
            {
                int pc;
                dt = ex.GetData("sp_xdagetsophieuchiconnotra_KhongVat");
                int sopc = Int16.Parse(dt.Rows[0]["sopc"].ToString());
                if (sopc == 0)
                    pc = 1;
                else pc = sopc + 1;
                p = new SqlParameter[13];
                p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                p[1] = new SqlParameter("@NhaCungCap", ex.GetGuid(drnhacungcap.SelectedValue));
                p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                p[2] = new SqlParameter("@SoDuKiTruoc", txtsodukitruoc.Text.Trim());
                p[2].SqlDbType = SqlDbType.Float;
                p[3] = new SqlParameter("@CuocVC", txttiencuoc.Text.Trim());
                p[3].SqlDbType = SqlDbType.Float;
                p[4] = new SqlParameter("@TienHang", txttienhang.Text.Trim());
                p[4].SqlDbType = SqlDbType.Float;
                p[5] = new SqlParameter("@SoTienThanhToanKiTruoc", txtsotiendathanhtoan.Text.Trim());
                p[5].SqlDbType = SqlDbType.Float;
                p[6] = new SqlParameter("@SoTienThanhToan", double.Parse(txtsotienthanhtoan.Text.Trim().Equals("") ? "0" : txtsotienthanhtoan.Text.Trim()));
                p[6].SqlDbType = SqlDbType.Float;
                p[7] = new SqlParameter("@HinhThucThanhToan", drhinhthucthanhtoan.SelectedValue);
                p[7].SqlDbType = SqlDbType.Int;
                p[8] = new SqlParameter("@SoPC", pc);
                p[8].SqlDbType = SqlDbType.Int;
                p[9] = new SqlParameter("@SoTaiKhoan", ex.GetGuid(drtaikhoan.SelectedValue));
                p[9].SqlDbType = SqlDbType.UniqueIdentifier;
                p[10] = new SqlParameter("@NgayThanhToan", ex.GetNgayThang(txtngaythanhtoan.Text));
                p[10].SqlDbType = SqlDbType.SmallDateTime;
                p[11] = new SqlParameter("@LoaiThanhToan", 2);
                p[11].SqlDbType = SqlDbType.Int;
                p[12] = new SqlParameter("@Del", 1);
                p[12].SqlDbType = SqlDbType.Int;
                ex.ExecuteNonQuerry("sp_XDAThanhToanCongNoTra_Add", p);

                p = new SqlParameter[9];
                p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythanhtoan.Text.Trim()));
                p[1].SqlDbType = SqlDbType.SmallDateTime;
                p[2] = new SqlParameter("@NoiDung", "ThanhToanCongNo");
                p[2].SqlDbType = SqlDbType.NVarChar;
                p[3] = new SqlParameter("@DonVi", ex.GetGuid(drnhacungcap.SelectedValue));
                p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                p[4] = new SqlParameter("@SoPC", pc);
                p[4].SqlDbType = SqlDbType.Int;
                p[5] = new SqlParameter("@TaiKhoan", ex.GetGuid(drtaikhoan.SelectedValue));
                p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                p[6] = new SqlParameter("@Del", 1);
                p[6].SqlDbType = SqlDbType.Int;
                p[7] = new SqlParameter("@LoaiCongNo", 2);
                p[7].SqlDbType = SqlDbType.Int;
                p[8] = new SqlParameter("@SoTien", txtsotienthanhtoan.Text.Trim());
                p[8].SqlDbType = SqlDbType.Int;
                ex.ExecuteNonQuerry("sp_XDAPhieuChiCongNo_Add", p);
                btnCancel_Click(sender, e);
                gstGetMess("Lưu thành công", "");
            }
            catch (Exception ax)
            {
                gstGetMess("Có lỗi trong quá trình lưu, xem trong file log", "");
                ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tThiết lập rút sec\t" + ax.Message);
            }
        }
        else
            gstGetMess("Bạn không có quyền thêm dữ liệu trên trang này", "");
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 1;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdathanhtoancongnotra_ListKhongVAT", p);
        GV.DataBind();



        btnPre.Enabled = false;
        btnNext.Enabled = true;
        btnFirst.Enabled = false;
    }
    protected void btnPre_Click(object sender, EventArgs e)
    {
        if (CurrentPage > 1)
        {
            CurrentPage--;
            btnPre.Enabled = true;
            btnNext.Enabled = true;
            btnFirst.Enabled = true;

            p = new SqlParameter[2];
            p[0] = new SqlParameter("@PageNumber", CurrentPage);
            p[0].SqlDbType = SqlDbType.Int;
            p[1] = new SqlParameter("@RowspPage", PageNumber);
            p[1].SqlDbType = SqlDbType.Int;

            GV.DataSource = ex.GetData("sp_xdathanhtoancongnotra_ListKhongVAT", p);
            GV.DataBind();



        }
        else
        {
            btnFirst.Enabled = false;
            btnPre.Enabled = false;
            btnNext.Enabled = true;
        }
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        CurrentPage++;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdathanhtoancongnotra_ListKhongVAT", p);
        GV.DataBind();



        if (GV.Rows.Count.Equals(0))
        {
            btnPre.Enabled = true;
            btnNext.Enabled = false;
        }
        else
        {
            btnPre.Enabled = true;
            btnNext.Enabled = true;
        }
        btnFirst.Enabled = true;
    }
    protected void btnXem_Click(object sender, EventArgs e)
    {
        if (dlnhacungcap.SelectedIndex == 0)
        {
            p = new SqlParameter[2];
            p[0] = new SqlParameter("@Thang", int.Parse(drthang.SelectedValue));
            p[0].SqlDbType = SqlDbType.Int;
            p[1] = new SqlParameter("@Nam", int.Parse(drnam.SelectedValue));
            p[1].SqlDbType = SqlDbType.Int;

            GV.DataSource = ex.GetData("sp_xdathanhtoancongnotra_List_TimKiemKhongNhaCungCap_KhongVAT", p);
            GV.DataBind();
        }
        else
        {
            p = new SqlParameter[3];
            p[0] = new SqlParameter("@Thang", int.Parse(drthang.SelectedValue));
            p[0].SqlDbType = SqlDbType.Int;
            p[1] = new SqlParameter("@Nam", int.Parse(drnam.SelectedValue));
            p[1].SqlDbType = SqlDbType.Int;
            p[2] = new SqlParameter("@NCC", ex.GetGuid(dlnhacungcap.SelectedValue));
            p[2].SqlDbType = SqlDbType.UniqueIdentifier;
            GV.DataSource = ex.GetData("sp_xdathanhtoancongnotra_List_TimKiem_KhongVAT", p);
            GV.DataBind();
        }

    }
}